package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Clinic_leaves_dao;
import com.clinic.domain.Clinic_leaves;
import com.clinic.util.Util_imp;

public class Clinic_leaves_imp implements Clinic_leaves_dao {

	SessionFactory session_factory = null;

	public Clinic_leaves_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_clinic_leave(Clinic_leaves cs) {
		System.out.println("Enter - create_clinic_leave");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int cs_obj_id = 0;
		try {
			tx = session.beginTransaction();
			cs_obj_id = (Integer) session.save(cs);
			tx.commit();
			System.out
					.println("New Clinic_leave Created with ID: " + cs_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Clinic_leave Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_clinic_leave");
		return cs_obj_id;
	}

	@Override
	public void update_clinic_leave(Clinic_leaves cs) {
		System.out.println("Enter - update_clinic_leave");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(cs);
			tx.commit();
			System.out.println("Clinic_leave Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Clinic_leave Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_clinic_leave");
	}

	@Override
	public void delete_clinic_leave(int cs_obj_id) {
		System.out.println("Enter - delete_clinic_leave");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Clinic_leaves cs = (Clinic_leaves) session.get(Clinic_leaves.class,
					cs_obj_id);
			session.delete(cs);
			tx.commit();
			System.out.println("Clinic_leave with ID: " + cs_obj_id
					+ " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Clinic_leave Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_clinic_leave");
	}

	@Override
	public Clinic_leaves get_clinic_leave(int cs_obj_id) {
		System.out.println("Enter - get_clinic_leave");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Clinic_leaves cs = null;
		try {
			tx = session.beginTransaction();
			cs = (Clinic_leaves) session
					.createQuery("from Clinic_leaves where cs_obj_id=:id")
					.setParameter("id", cs_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Status: "
					+ cs.getLeave_status());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_clinic_leave");
		return cs;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Clinic_leaves> get_all_clinic_leaves() {
		System.out.println("Enter - get_all_clinic_leaves");
		Session session = session_factory.openSession();
		List<Clinic_leaves> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Clinic_leaves").list();
			tx.commit();
			System.out.println("Number of Clinic_leaves returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_clinic_leaves");
		return l;
	}

	@Override
	public Clinic_leaves persistOrMerge(Clinic_leaves cs) {
		return (Clinic_leaves) session_factory.getCurrentSession().merge(cs);
	}
	
	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Clinic_leaves").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Clinic_leaves: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}